<script setup lang="ts">
import { reactive, ref } from 'vue'
import { User, Lock } from '@element-plus/icons-vue' //图标
import useUserStore from '@/store/modules/user'
import { useRouter, useRoute } from 'vue-router'
import { ElNotification } from 'element-plus' //消息提示框

const form = ref()
const loading = ref(false)
const router = useRouter()
const route = useRoute()
//登录密码
let loginForm = reactive({ username: 'admin', password: 'atguigu123' })

//自定义校验规则
const validatorUserName = (rule: any, value: any, callback: any) => {
  // console.log(rule)
  if (value.length > 4) {
    callback()
  } else {
    callback(new Error('账号长度至少为5'))
  }
}
//自定义校验规则
const validatorPassword = (rule: any, value: any, callback: any) => {
  if (value.length > 5) {
    callback()
  } else {
    callback(new Error('密码长度至少为6'))
  }
}
const rules = {
  // 规则属性
  // trigger: 触发校验表单的时机，change->文本发生变化触发校验， blur：失去焦点触发的校验
  username: [
    // {
    //   required: true,
    //   min: 6,
    //   max: 10,
    //   message: '账号长度至少六位',
    //   trigger: 'change',
    // },
    { tigger: 'change', validator: validatorUserName },
  ],
  password: [
    // {
    //   required: true,
    //   min: 6,
    //   max: 14,
    //   message: '密码场地为6-14位数',
    //   trigger: 'change',
    // },
    { tigger: 'change', validator: validatorPassword },
  ],
}

//用户信息仓库
let useStore = useUserStore()

// 点击登录按钮执行登录操作
const login = async () => {
  // let validate = form.value.validate()
  // console.log(validate)
  loading.value = true
  try {
    //假token
    // localStorage.setItem('TOKEN', ' Admin_Token')
    await useStore.userLogin(loginForm)
    // 登录之后保存重定向信息
    let redirect: any = route.query.redirect
    router.push({ path: redirect || '/' })
    ElNotification({
      type: 'success',
      message: '欢迎回来',
    })
  } catch (error) {
    loading.value = false //登陆失败
    ElNotification({
      type: 'error',
      message: (error as Error).message, //error as Error ---> 断言错误类型
    })
  }
}
</script>

<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <!-- 登录的表单 -->
        <el-form
          class="login_form"
          :model="loginForm"
          :rules="rules"
          ref="form"
        >
          <h1>Hello</h1>
          <h2>欢迎来到硅谷甄选</h2>
          <el-form-item prop="username">
            <el-input
              :prefix-icon="User"
              v-model="loginForm.username"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              type="password"
              :prefix-icon="Lock"
              show-password
              v-model="loginForm.password"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              :loading="loading"
              class="login_btn"
              type="primary"
              size="default"
              @click="login"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<style scoped lang="scss">
.login_container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;

  .login_form {
    position: relative;
    width: 80%;
    top: 30vh;
    background: url('@/assets/images/login_form.png') no-repeat;
    background-size: cover;
    padding: 40px;

    h1 {
      color: white;
      font-size: 40px;
    }

    h2 {
      font-size: 20px;
      color: white;
      margin: 20px 0px;
    }

    .login_btn {
      width: 100%;
    }
  }
}
</style>
